#coding:utf-8
from rest_framework.generics import CreateAPIView,GenericAPIView
from rest_framework import mixins
from erp_system.models import UserModel
from rest_framework.viewsets import GenericViewSet

from MSB_ERP.apps.erp_system.serializer.user_serializer import UserRegisterSerializer, UserUpdateOrDeleteSerializer, \
    UserGetSerializer, ResetPasswordSerializer
from MSB_ERP.utils.base_views import MultipleDestroyMixin
from MSB_ERP.utils.pagination import GlobalPagination


class RegisterView(CreateAPIView):


    queryset = UserModel.objects.all()
    serializer_class = UserRegisterSerializer




#当前视图类，除了用户注册和修改密码之外，其他所有的用户管理操作
class UserView(mixins.UpdateModelMixin,
               mixins.RetrieveModelMixin,
               mixins.DestroyModelMixin,
               mixins.ListModelMixin,
               MultipleDestroyMixin,
               GenericViewSet):
    queryset = UserModel.objects.all()

    pagination_class = GlobalPagination

    def get_serializer_class(self):
        #如果是修改和删除的操作，使用一个统一的序列化，不同的业务需要的属性不同所以需要不同的序列化，
        if self.action=='partial_update' or self.action=='update' or self.action=='destroy':
            return UserUpdateOrDeleteSerializer
        else:
            return UserGetSerializer

class ResetPasswordView(mixins.UpdateModelMixin,GenericViewSet):
    #用户重置密码，需要输入原密码，新密码和新密码的确认
    queryset = UserModel.objects.all()
    serializer_class = ResetPasswordSerializer
    def patch(self,request,*args,**kwargs):
        return self.partial_update(request,*args,**kwargs)






























